package com.cloudbroker.bcs.platform.ltb.middle.util;

import com.alibaba.fastjson.JSON;
import com.cloudbroker.bcs.platform.ltb.common.util.CacheHelper;
import com.cloudbroker.bcs.platform.ltb.common.util.SessionHelper;
import com.cloudbroker.bcs.platform.ltb.middle.entity.credits.CreditsRule;
import com.cloudbroker.bcs.platform.ltb.middle.entity.credits.TaskProgress;
import org.apache.commons.lang.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TreeSet;

/**
 * Created by lili19289 on 2016/11/10.
 */
public class CreditsCacheUtil {


    private static CacheHelper cacheHelper = CacheHelper.getInstance();

    /**
     * 相关积分增加情况
     * @param user_id
     * @param business_type
     * @param value
     */
    public static void addUserCache(long user_id,int business_type,String company_id,int value ){
        String key="credits_current_value"+company_id+user_id+business_type;
        String currentValue = cacheHelper.get(key);
        if(StringUtils.isBlank(currentValue)){
            cacheHelper.set(key,value+"",getExpireTime());
        }else{
            int current = Integer.parseInt(currentValue)+value;
            cacheHelper.set(key,current+"",getExpireTime());
        }
    }

    public static int getUserCache(long user_id,int business_type,String company_id){
        String key="credits_current_value"+company_id+user_id+business_type;
        String current_value =cacheHelper.get(key);
        if(StringUtils.isBlank(current_value)){
            return 0;
        }else {
            return Integer.parseInt(current_value);
        }
    }

    /**
     * 当天增加总积分
     * @param user_id
     * @param company_id
     * @param value
     */
    public static void addTotalCache(long user_id,String company_id,int value ){
        String key="credits_total_value"+company_id+user_id;
        String currentValue = cacheHelper.get(key);
        if(StringUtils.isBlank(currentValue)){
            cacheHelper.set(key,value+"",getExpireTime());
        }else{
            int current = Integer.parseInt(currentValue)+value;
            cacheHelper.set(key,current+"",getExpireTime());
        }
    }

    public static int getTotalCache(long user_id,String company_id){
        String key="credits_total_value"+company_id+user_id;
        String current_value =cacheHelper.get(key);
        if(StringUtils.isBlank(current_value)){
            return 0;
        }else {
            return Integer.parseInt(current_value);
        }
    }

    /**
     * 相关业务下积分增加次数
     * @param user_id
     * @param business_type
     * @param company_id
     */
    public static void addCreditsCount(long user_id,int business_type,String company_id){
        String key="credits_current_count"+company_id+user_id+business_type;
        String currentCount = cacheHelper.get(key);
        if(StringUtils.isBlank(currentCount)){
            cacheHelper.set(key,"1",getExpireTime());
        }else{
            int current = Integer.parseInt(currentCount)+1;
            cacheHelper.set(key,current+"",getExpireTime());
        }
    }

    public static int getCreditsCount(long user_id,int business_type,String company_id){
        String key="credits_current_count"+company_id+user_id+business_type;
        String currentCount = cacheHelper.get(key);
        if(StringUtils.isBlank(currentCount)){
            return 0;
        }else{
            return Integer.parseInt(currentCount);
        }
    }

    /**
     * 相关积分规则
     * @param creditsRule
     */
    public static  void addRuleCache(CreditsRule creditsRule ){
        String key="credits_rule_"+creditsRule.getCompany_id()+creditsRule.getBusiness_type();
        cacheHelper.set(key, JSON.toJSONString(creditsRule),getExpireTime(1));
    }

    public static CreditsRule  getRuleCache(String company_id,int business_type){
        String key="credits_rule_"+company_id+business_type;
        String creditsRule= cacheHelper.get(key);
        if(StringUtils.isBlank(creditsRule)){
            return null;
        }else{
            return JSON.parseObject(creditsRule,CreditsRule.class);
        }

    }

    public static void addLoginTime(long user_id){
        String key="loginTime_"+user_id;
        String lasting_days = cacheHelper.get(key);
        if(StringUtils.isBlank(lasting_days)){
            cacheHelper.set(key,"1",getExpireTime());
        }else{
            cacheHelper.set(key,(Integer.parseInt(lasting_days)+1)+"",getExpireTime());
        }

    }
    public static boolean isLoginded(long user_id){
        String key="loginTime_"+user_id;
        String loginTime = cacheHelper.get(key);
        if(StringUtils.isBlank(loginTime)){
            return false;
        }else{
            return true;
        }
    }



    //任务进度
    public static void addTaskProgress(long user_id,TaskProgress taskProgress,int days){
        String key = "credits_task_progress"+user_id+taskProgress.getCompany_id()+taskProgress.getTask_id();
        cacheHelper.set(key, JSON.toJSONString(taskProgress),getExpireTime(days));
    }
    public static void updateTaskProgress(long user_id,TaskProgress taskProgress){
        String key = "credits_task_progress"+user_id+taskProgress.getCompany_id()+taskProgress.getTask_id();
        cacheHelper.update(key, JSON.toJSONString(taskProgress));
    }
    public static void updateTaskProgress(long user_id,TaskProgress taskProgress,int days){
        String key = "credits_task_progress"+user_id+taskProgress.getCompany_id()+taskProgress.getTask_id();
        cacheHelper.set(key, JSON.toJSONString(taskProgress),getExpireTime(days));
    }


    public static TaskProgress getTaskProgress(long user_id,String company_id,long task_id){
        String key = "credits_task_progress"+user_id+company_id+task_id;
        String taskProgress= cacheHelper.get(key);
        if(StringUtils.isBlank(taskProgress)){
            return null;
        }else{
            return JSON.parseObject(taskProgress,TaskProgress.class);
        }
    }
    public static void deleteTaskProgress(long user_id,TaskProgress taskProgress){
        String key = "credits_task_progress"+user_id+taskProgress.getCompany_id()+taskProgress.getTask_id();
        delete(key);
    }


    public static void addCreditOper(long user_id, int business_type, String id){
        String key="credits_credits_oper"+user_id+business_type;
        cacheHelper.sdd(key,id);
//        cacheHelper.expire(key,-1);

    }

    public static boolean isExitCreditsOper(long user_id, int business_type, String id){
        String key="credits_credits_oper"+user_id+business_type;
        return cacheHelper.sismember(key,id);
    }


    /**
     * 积分上限值
     * @param company_id
     * @param value
     */
    public static void addLimitCache(String company_id,int value ){
        String key="credits_limit_value"+company_id;
        cacheHelper.set(key,value+"",getExpireTime());
    }

    public static void deleteLimitCache(String company_id,int value ){
        String key="credits_limit_value"+company_id;
        delete(key);
    }

    public static int getLimitCache(String company_id){
        String key="credits_limit_value"+company_id;
        String limitValue = cacheHelper.get(key);
        if(StringUtils.isBlank(limitValue)){
            return 0;
        }else {
            return Integer.parseInt(limitValue);
        }
    }

    /**
     * 获取到当前时间到当天结束的时间差（单位：ms)
     * @return
     */
    public static int getExpireTime(){
        return getExpireTime(0);
    }

    /**
     * 获取相应过期时间戳，暂时只有天
     * redis过期时间是秒
     * @param days
     * @return
     */
    public static int getExpireTime(int days){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH,  days);
        calendar.set(Calendar.HOUR_OF_DAY , 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(calendar.getTime()));
        return (int) (calendar.getTimeInMillis()-System.currentTimeMillis())/1000;
    }

    public static  Long getStartTime(){
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime().getTime();
    }

    public static Long getEndTime(){
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime().getTime();
    }



    public static void delete(String key){
        cacheHelper.delete(key);
    }




    public static void main(String[] args) {
        System.out.println(getExpireTime());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(getStartTime()));
        System.out.println(sdf.format(getEndTime()));
//        String time=sdf.format(new Date());
//        Calendar cd = Calendar.getInstance();
//
//        try {
//            cd.setTime(sdf.parse(time));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        cd.add(Calendar.DATE, 1);//增加一天
//        //cal.add(Calendar.DATE, -1);      //减一天
//        //cd.add(Calendar.MONTH, 1);//增加一月
//        Date date=cd.getTime();
//        System.out.println(sdf.format(date));
    }
}
